package com.gxa.modules.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.base.RespResult;
import com.gxa.common.constants.Constants;
import com.gxa.modules.dto.AddressEmployeeDTO;
import com.gxa.modules.dto.AdvancedSearchDTO;
import com.gxa.modules.mapper.AddressServiceMapper;
import com.gxa.modules.service.AddressService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

@Slf4j
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressServiceMapper addressServiceMapper;
    @Override
    public RespResult findEmployeeByDepart(Integer type, Integer currentPage, Integer pageSize) {
        Page<AddressEmployeeDTO> iPage=new Page<>(currentPage-1,pageSize);
        Map map=new HashMap<>();
        Page<AddressEmployeeDTO> list = null;
        if(type==0){
           list=addressServiceMapper.findAll(iPage, Constants.ON_THE_JOB);
        }else if(type==9){
            list=addressServiceMapper.findAll(iPage,Constants.DEPARTURE);
        }else if(type>0 && type<9) {
            list = addressServiceMapper.findEmployeeByDepart(iPage, type);
        }else {
            return RespResult.error("没有数据");
        }

        map.put("total",list.getTotal());
        map.put("emp",list.getRecords());
        return RespResult.success(map);
    }

    @Override
    public RespResult search(Integer currentPage, Integer pageSize, Integer type, String value) {
        Page<AddressEmployeeDTO> iPage=new Page<>(currentPage-1,pageSize);
        Map map=new HashMap<>();
        Page<AddressEmployeeDTO> list = null;
        if(type==0){
            list=addressServiceMapper.searchIn(iPage, Constants.ON_THE_JOB,value);
        }else if(type==9){
            list=addressServiceMapper.searchIn(iPage,Constants.DEPARTURE,value);
        }else if(type>0 && type<9) {
            list = addressServiceMapper.search(iPage,type,value);
        }else {
            return RespResult.error("没有数据");
        }
        map.put("total",list.getTotal());
        map.put("emp",list.getRecords());
        return RespResult.success(map);
    }

    @Override
    public RespResult advancedSearch(AdvancedSearchDTO advancedSearchDTO) {
        Page<AddressEmployeeDTO> iPage=new Page<>(advancedSearchDTO.getCurrentPage()-1,advancedSearchDTO.getPageSize());
        Map map=new HashMap<>();
        Page<AddressEmployeeDTO> list = null;
        if(advancedSearchDTO!=null){
            list=addressServiceMapper.advancedSearchIn(iPage,advancedSearchDTO);;
        }else {
            return RespResult.error("数据为空");
        }
        map.put("total",list.getTotal());
        map.put("emp",list.getRecords());
        return RespResult.success(map);
    }
}
